package com.infomanage.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.infomanage.bean.FieldBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.exception.DaoException;
import com.infomanage.po.AssessZdDept;
import com.infomanage.po.Department;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.vo.AssessZdDeptItem;

public class AssessZdDeptDAO extends BaseDAO<AssessZdDept>{
	public AssessZdDeptItem find(int id) throws DaoException{
		try {
			SearchBean searchBean  = new SearchBean();
			FieldBean fieldBean  = new FieldBean();
			fieldBean.setName("assessDeptId");
			fieldBean.setValue(id);
			searchBean.add(fieldBean);
			List result = super.findByFields(new Class[] {Department.class },
					AssessZdDept.class, new String[] {"deptId" },
					new String[] {"deptId" }, searchBean);
			List<AssessZdDeptItem> items = parseResult(result);
			return items.size() == 0 ? null : items.get(0);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	@SuppressWarnings("unchecked")
	public List<AssessZdDeptItem> findAll() throws DaoException {
		try {
			List result = findAllMutilTable(new Class[] {Department.class },
					AssessZdDept.class, new String[] {"deptId" },
					new String[] {"deptId" });
			return parseResult(result);
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<AssessZdDeptItem> findByFields(SearchBean searchBean) throws DaoException {
		List result = super.findByFields(new Class[] {Department.class },
				AssessZdDept.class, new String[] {"deptId" },
				new String[] {"deptId" }, searchBean);
		return parseResult(result);
	}
	
	public List<AssessZdDeptItem> parseResult(List result){
		Iterator it = result.iterator();
		List<AssessZdDeptItem> list = new ArrayList<AssessZdDeptItem>();
		while (it.hasNext()) {
			Object[] o = (Object[]) it.next();
			AssessZdDept assess = (AssessZdDept) o[0];
			Department dept = (Department) o[1];
			AssessZdDeptItem item = new AssessZdDeptItem();
			item.setBean(assess);
			item.setDept(dept);
			list.add(item);
		}
		return list;
	}
	
	public int count(SearchBean searchBean){
		return super.count(new Class[] {Department.class },
				AssessZdDept.class, new String[] {"deptId" },
				new String[] {"deptId" }, searchBean);
	}
}